package com.hmyuan.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hmyuan.dao.Sys_res_userMapper;
import com.hmyuan.dao.Sys_userMapper;
import com.hmyuan.dao.Sys_user_roleMapper;
import com.hmyuan.domain.Sys_res_userKey;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.domain.Sys_user_roleKey;
import com.hmyuan.service.SysUserService;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

	@Resource
	Sys_userMapper sys_userMapper;
	@Resource
	Sys_user_roleMapper sys_user_roleMapper;
	@Resource
	Sys_res_userMapper sys_res_userMapper;
	
	@Override
	public Sys_user selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return sys_userMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean insert(Sys_user sys_user) {
		// TODO Auto-generated method stub
		int num = sys_userMapper.insert(sys_user);
		return num==1?true:false;
	}

	@Override
	public List<Sys_user> selectList(Sys_user sys_user) {
		// TODO Auto-generated method stub
		return sys_userMapper.selectList(sys_user);
	}

	@Override
	public PageInfo<Sys_user> selectListByPage(Sys_user sys_user, int pageNum,
			int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize); // 核心分页代码
		List<Sys_user> list = sys_userMapper.selectList(sys_user);
		PageInfo<Sys_user> pageInfo = new PageInfo<Sys_user>(list);
		
		return pageInfo;
	}

	@Override
	public boolean updateByPrimaryKeySelective(Sys_user record) {
		// TODO Auto-generated method stub
		int num = sys_userMapper.updateByPrimaryKeySelective(record);
		return num>0?true:false;
	}

	@Override
	public boolean insertUserRole(Sys_user_roleKey sys_user_role) {
		// TODO Auto-generated method stub
		int num = sys_user_roleMapper.insert(sys_user_role);
		return num==1?true:false;
	}

	@Override
	public boolean deleteUserRole(Sys_user_roleKey sys_user_role) {
		// TODO Auto-generated method stub
		int num = sys_user_roleMapper.delete(sys_user_role);
		return num==1?true:false;
	}

	@Override
	public boolean insertResUsers(Long userId, Long[] resIds) {
		// TODO Auto-generated method stub
		//添加前清空用户权限
		sys_res_userMapper.deleteByUserid(userId);
		
		if(null==resIds||resIds.length==0) return true;
		
		Sys_res_userKey sys_res_user = new Sys_res_userKey();
		sys_res_user.setUserid(userId);
		for (long i : resIds) {
			sys_res_user.setResid(i);
			sys_res_userMapper.insert(sys_res_user);
		}
		return true;
	}

	@Override
	public Sys_user selectListByAccountname(String accountname) {
		// TODO Auto-generated method stub
		Sys_user sys_user = new Sys_user();
		sys_user.setAccountname(accountname);
		List<Sys_user> list = sys_userMapper.selectList(sys_user);
		return list.size()==1?list.get(0):null;
	}

}
